<template>
  <div class="app-container">
    <Find :OriginalFilter="{ static: true }" :searchImg="false" />
    <!-- 主体 -->
    <el-card class="ProductionDynamicMainCard" shadow="always" :body-style="{ padding: '15px' }">
      <div class="ProductionDynamicMainCardMain">
        <div style="width: 40%">
          <h1 class="H1Size">
            代码类型 <el-button type="primary" size="small" @click="dialogVisible1 = true">增</el-button>
            <el-button type="primary" size="small" @click="handleCloseMateriaLKlc5">删</el-button>
          </h1>
          <Table
            :isLoading="isLoading2"
            @inputChange="checkboxChange1"
            @currentRow="currentRow3"
            :tableData="TypeQueryTableDate"
            :tableHeader="tableHeader"
            :selection="false"
            :TableHeight="100"
          />
        </div>
        <div style="width: 40%; margin-left: 30px">
          <h1 class="H1Size" style="margin-right: 15px">
            代码明细<el-button type="primary" size="small" @click="dialogVisible2 = true">增</el-button>
            <el-button type="primary" size="small" @click="handleCloseMateriaLKlc6">删</el-button>
          </h1>
          <div>
            <Table
              :isLoading="isLoading"
              @inputChange="checkboxChange2"
              @currentRow="currentRow1"
              :tableData="typeListTableDate"
              :tableHeader="tableHeader1"
              :selection="false"
              :TableHeight="100"
            />
          </div>
        </div>
      </div>
    </el-card>

    <el-dialog v-model="dialogVisible1" width="50%">
      <Find :OriginalFilter="{ static: true }" @Launchform="Launchform11" @findClicked="findClicked11" :searchImg="false" :formList="formList12" />
    </el-dialog>

    <el-dialog v-model="dialogVisible2" width="50%">
      <Find :OriginalFilter="{ static: true }" @Launchform="Launchform12" @findClicked="findClicked12" :searchImg="false" :formList="formList13" />
    </el-dialog>
  </div>
</template>
<script lang="ts">
import { defineComponent, ref, onMounted } from 'vue'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'ToolInformation'
})
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
import {
  deletetypeListQueryList,
  AddtypeListQueryList,
  deletetypeListQuery,
  TypeQuery,
  typeListQuery,
  AddtypeListQuery
} from '@/api/Control/FactoryControl/ToolInformation'
import LoginWork from '@/utils/LoginWork'
import { ElMessage, ElMessageBox } from 'element-plus'

const isLoading2 = ref(false)

const isLoading = ref(false)

// 删除
const deletetypeListQueryListParms = ref({
  baseType: '',
  itemCode: ''
})

const deletetypeListQueryListMethods = async () => {
  const result = await deletetypeListQueryList(deletetypeListQueryListParms.value)
  if (result.code === 200) {
    typeListQueryMethods()
    deletetypeListQueryListParms.value.baseType = ''
    JMessage.success(result.message)
  } else {
    JMessage.error(result.message)
  }
}

const handleCloseMateriaLKlc6 = () => {
  if (deletetypeListQueryListParms.value.baseType === '') {
    JMessage.warning('请选择需要删除的数据')
  } else {
    ElMessageBox.confirm('确认要删除数据吗?')
      .then(async () => {
        deletetypeListQueryListMethods()
      })
      .catch(() => {})
  }
}

// 代码明细新增
const dialogVisible2 = ref(false)

const AddtypeListQueryListParms = ref({
  baseType: '',
  companyId: '',
  itemCode: '',
  itemName: '',
  itemType: '',
  seqNo: -1
})

const findClicked12 = (row: any) => {
  if (row === '保存') {
    if (AddtypeListQueryListParms.value.seqNo === -1 || AddtypeListQueryListParms.value.itemCode === '') {
      JMessage.warning('序号和代码不可不可为空')
    } else {
      AddtypeListQueryListMethods()
    }
  }
  if (row === '置空') {
    clearFormList3()
  }
}

const Launchform12 = (row: any) => {
  AddtypeListQueryListParms.value.baseType = row[0].inputValue
  AddtypeListQueryListParms.value.companyId = row[1].inputValue
  AddtypeListQueryListParms.value.itemCode = row[2].inputValue
  AddtypeListQueryListParms.value.itemName = row[3].inputValue
  AddtypeListQueryListParms.value.itemType = row[4].inputValue
  AddtypeListQueryListParms.value.seqNo = Number(row[5].inputValue)
}

const AddtypeListQueryListMethods = async () => {
  const result = await AddtypeListQueryList(AddtypeListQueryListParms.value)
  if (result.code === 200) {
    typeListQueryMethods()
    clearFormList3()
    dialogVisible2.value = false
    JMessage.success(result.message)
  } else {
    JMessage.error(result.message)
  }
}

const clearFormList3 = () => {
  formList13.value.forEach((item) => {
    if (item.type === 'input') {
      item.inputValue = ''
    }
  })
}

// 代码明细删除
const checkboxChangeParms = ref({
  baseTypeOld: '',
  itemCodeOld: ''
})

const checkboxChange2 = async (list: any) => {
  const result = await LoginWork.put<any, any>(
    `/bioDeptWrok/cpBaseDetUpdate?baseTypeOld=${checkboxChangeParms.value.baseTypeOld}&itemCodeOld=${checkboxChangeParms.value.itemCodeOld}`,
    list
  )
  if (result.code === 200) {
    typeListQueryMethods()
    JMessage.success(result.message)
  } else {
    JMessage.error(result.message)
  }
}

// 增 删
const deletetypeListQueryMethodsParms = ref({
  baseType: ''
})

const deletetypeListQueryMethods = async () => {
  const result = await deletetypeListQuery(deletetypeListQueryMethodsParms.value)
  if (result.code === 200) {
    TypeQueryMethods()
    deletetypeListQueryMethodsParms.value.baseType = ''
    JMessage.success(result.message)
  } else {
    JMessage.error(result.message)
  }
}

const handleCloseMateriaLKlc5 = () => {
  if (deletetypeListQueryMethodsParms.value.baseType === '') {
    JMessage.warning('请选择需要删除的数据')
  } else {
    ElMessageBox.confirm('确认要删除数据吗?')
      .then(async () => {
        deletetypeListQueryMethods()
      })
      .catch(() => {})
  }
}

const AddtypeListParms = ref({
  baseName: '',
  baseType: ''
})

const clearFormList2 = () => {
  formList12.value.forEach((item) => {
    if (item.type === 'input') {
      item.inputValue = ''
    }
  })
}

const AddtypeListQueryMethods = async () => {
  const result = await AddtypeListQuery(AddtypeListParms.value)
  if (result.code === 200) {
    TypeQueryMethods()
    clearFormList2()
    dialogVisible1.value = false
    JMessage.success(result.message)
  } else {
    JMessage.error(result.message)
  }
}

const findClicked11 = (row: any) => {
  if (row === '保存') {
    if (AddtypeListParms.value.baseType === '') {
      JMessage.warning('代码类型不可为空')
    } else {
      AddtypeListQueryMethods()
    }
  }
  if (row === '置空') {
    clearFormList2()
  }
}

const Launchform11 = (row: any) => {
  AddtypeListParms.value.baseType = row[0].inputValue.toUpperCase()
  AddtypeListParms.value.baseName = row[1].inputValue
}

const formList12 = ref([
  {
    type: 'input',
    inputPlaceholder: '类型代码',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'input',
    inputPlaceholder: '类型名称',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'button',
    checkboxLabel: '保存'
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  }
])

const formList13 = ref([
  {
    type: 'input',
    inputPlaceholder: '所属类型',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'input',
    inputPlaceholder: '公司ID',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'input',
    inputPlaceholder: '代码',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'input',
    inputPlaceholder: '名称',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'input',
    inputPlaceholder: '分类',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'input',
    inputPlaceholder: '序号',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'button',
    checkboxLabel: '保存'
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  }
])

const dialogVisible1 = ref(false)

// 代码类型修改

const currentRow1 = (row: any) => {
  checkboxChangeParms.value.baseTypeOld = row.baseType
  checkboxChangeParms.value.itemCodeOld = row.itemCode

  deletetypeListQueryListParms.value.baseType = row.baseType
  deletetypeListQueryListParms.value.itemCode = row.itemCode
}

const checkboxChange1 = async (list: any) => {
  const result = await LoginWork.put<any, any>(`/bioDeptWrok/cpBaseHeadUpdate?baseTypeOld=${typeListQueryParms.value}`, list)
  if (result.code === 200) {
    TypeQueryMethods()
    JMessage.success(result.message)
  } else {
    TypeQueryMethods()
    JMessage.error(result.message)
  }
}

// 代码明细查询
const typeListQueryParms = ref('')

const typeListTableDate = ref([])

const currentRow3 = (row: any) => {
  typeListQueryParms.value = row.baseType
  deletetypeListQueryMethodsParms.value.baseType = row.baseType
  typeListQueryMethods()
}

const typeListQueryMethods = async () => {
  isLoading.value = true
  const result = await typeListQuery(typeListQueryParms.value)
  typeListTableDate.value = result.map((item: any) => {
    item.change = 'Y'
    item.disableConfirm = true
    return item
  })
  isLoading.value = false
}

// 代码类型查询

const TypeQueryTableDate = ref([])

const TypeQueryMethods = async () => {
  isLoading2.value = true
  const result = await TypeQuery()
  TypeQueryTableDate.value = result.map((item: any) => {
    item.change = 'Y'
    item.disableConfirm = true
    return item
  })
  if (result.length > 0) {
    typeListQueryParms.value = result[0].baseType
    typeListQueryMethods()
  }
  isLoading2.value = false
}

onMounted(() => {
  TypeQueryMethods()
})

// 表格数据

const tableHeader = ref([
  {
    label: '类型代码',
    prop: 'baseType'
  },
  {
    label: '类型名称',
    prop: 'baseName'
  }
])

// 表格数据

const tableHeader1 = ref([
  {
    label: '代码',
    prop: 'itemCode'
  },
  {
    label: '名称',
    prop: 'itemName'
  },
  {
    label: '分类',
    prop: 'itemType'
  },
  {
    label: '顺序号',
    prop: 'seqNo'
  }
])
</script>

<style lang="less" scoped>
.ProductionDynamicMainCardMain {
  display: flex;
  // justify-content: space-between;
}

.H1Size {
  font-size: 16px;
  justify-content: space-between;
}

.bottom {
  float: right;
  margin-top: 10rem;
}
</style>
